import shop from '../../api/shop'
import * as types from '../mutation-types'

// initial state
const state = {
  all: []
}

// getters
const getters = {
  allProducts: state => state.all
}

// actions
const actions = {
  getAllProducts ({ commit }) {
    shop.getProducts(products => {
      commit(types.RECEIVE_PRODUCTS, { products })
    })
  },

  justDoIt ({ commit },product) {
    commit(types.ADD_TO_CART, {id: product.id})
  }


}

// mutations
const mutations = {
  [types.RECEIVE_PRODUCTS] (state, { products }) {
    state.all = products
  },

  [types.ADD_TO_CART] (state, { id }) {
    console.log("1111")
    state.all.find(p => p.id === id).inventory --
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
/*

  const actions = {
    getAllProducts ({ commit }) {
      shop.getProducts(products => {
        commit(types.RECEIVE_PRODUCTS,{ products })
      })
    }
  }



  const mutations = {
    [type.RECEIVE_PRODUCTS] (state,{ products }) {
      state.all = products
    }
  }
*/
